package com.zhilianbao.leyaogo.ui.adapter.home.grids;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhilianbao.leyaogo.R;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 首页栏目广告 ItemViewProvider
 */
public class HomeAppGridsViewProvider
        extends ItemViewProvider<HomeAppGrids, HomeAppGridsViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_home_column_test, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull ViewHolder holder, @NonNull HomeAppGrids homeAppGrids) {
        holder.setData(homeAppGrids.getData().getList());

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //RecyclerView
        private RecyclerView mRecyclerView;
        //适配器
        private HomeAppGirdsAdapter mHomeColumnAdapter;

        ViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.rcv_column);
            mHomeColumnAdapter = new HomeAppGirdsAdapter(mRecyclerView.getContext(), new ArrayList<>());
            mRecyclerView.setLayoutManager(new GridLayoutManager(mRecyclerView.getContext(), 4));
            mRecyclerView.setAdapter(mHomeColumnAdapter);
        }

        /**
         * 设置数据
         *
         * @param list
         */
        private void setData(List<HomeAppGrids.HomeAppGridsData.HomeAppGridsBean> list) {
            mHomeColumnAdapter.replaceAll(list);

        }
    }
}